import os
from time import sleep

import allure
import pytest

import page
from page.page_jiamingceshi import PageJiamingceshi
from page.page_login import PageLogin
from tool.get_driver import GetDriver
from tool.get_log import GetLogger

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]


@allure.epic("五十音页面")
@allure.feature("假名测试测试用例")
class TestJiamingceshi():
    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        cls.jiaming = PageJiamingceshi(cls.driver)
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass
        try:
            cls.login.base_click_element(page.skip_user_desc)
        except:
            pass
        cls.login.pagelogin(os.environ['JIAMINGCESHI_USERNAME'], '123456')
        cls.login.base_click_element(page.wsy_tab)

    @classmethod
    def teardown_class(cls):
        GetDriver.quit_app_driver()

    @pytest.mark.skipif(condition=bool(int(os.environ['SKIP_WSY_ONLINE'])), reason='跳过正式站测试开关打开，跳过正式站用例')
    @allure.story("假名测试")
    def test01_jiamingceshi(self):
        self.jiaming.pagejiamingceshi_click_start_study()
        self.jiaming.pagejiamingceshi_click_ceshi()
        while True:
            try:
                self.jiaming.pagejiamingceshi_click_fixed_answer()
            except:
                pass
            if self.jiaming.pagejiamingceshi_if_confirm_exist():
                self.jiaming.pagejiamingceshi_click_confirm()
                sleep(1)
                if self.jiaming.pagejiamingceshi_if_next_question_exist():
                    self.jiaming.pagejiamingceshi_click_next_question()
            if self.jiaming.pagejiamingceshi_if_question_exist():
                pass
            else:
                break

        try:
            log.info('正在进行断言')
            assert self.jiaming.base_find_element(page.study1_share_report)
            log.info('断言成功')
            self.jiaming.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.jiaming.base_get_screenshot_as_file(module_name, '假名测试未通过')
            raise

    @pytest.mark.skipif(bool(int(os.environ['SKIP_WSY_DEBUG'])), reason='跳过测试站开关打开，跳过测试站用例')
    @allure.story("假名测试")
    def test02_jiamingceshi_debug(self):
        self.jiaming.pagejiamingceshi_click_start_study()
        self.jiaming.pagejiamingceshi_click_ceshi()
        while True:
            try:
                self.jiaming.pagejiamingceshi_get_ans()
                sleep(1)
            except:
                pass
            if self.jiaming.pagejiamingceshi_if_question_exist():
                try:
                    if self.jiaming.pagejiamingceshi_if_confirm_exist():
                        self.jiaming.pagejiamingceshi_click_confirm()
                        sleep(2)
                except:
                    pass

            else:
                break
        try:
            log.info('正在进行断言')
            assert self.jiaming.base_find_element(page.study1_share_report)
            log.info('断言成功')
            self.jiaming.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.jiaming.base_get_screenshot_as_file(module_name, '假名测试未通过')
            raise

    @pytest.mark.skipif(condition=bool(int(os.environ['SKIP_WSY_DEBUG'])), reason='跳过测试站开关打开，跳过测试站用例')
    @allure.story("假名解锁测试用例")
    def test03_jiamingceshi_unlocked_debug(self):
        self.jiaming.pagejiamingceshi_click_share_report_back()
        try:
            log.info('正在进行断言')
            assert self.jiaming.pagejiamingceshi_if_unlocked_success()
            log.info('断言成功')
            self.jiaming.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.jiaming.base_get_screenshot_as_file(module_name, '假名测试解锁失败')
            raise

    @pytest.mark.skipif(condition=bool(int(os.environ['SKIP_WSY_DEBUG'])), reason='跳过测试站开关打开，跳过测试站用例')
    @allure.story("假名复习测试用例")
    def test04_jiamingceshi_fuxi_debug(self):
        self.jiaming.pagejiamingceshi_return_to_wsypage_keycode()
        self.jiaming.pagejiamingceshi_fuxi()
        while True:
            try:
                self.jiaming.pagejiamingceshi_get_ans()
                sleep(1)
            except:
                pass
            if self.jiaming.pagejiamingceshi_if_question_exist():
                try:
                    if self.jiaming.pagejiamingceshi_if_confirm_exist():
                        self.jiaming.pagejiamingceshi_click_confirm()
                        sleep(2)
                except:
                    pass
            else:
                break
        try:
            log.info('正在进行断言')
            assert self.jiaming.base_find_element(page.study1_share_report)
            log.info('断言成功')
            self.jiaming.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.jiaming.base_get_screenshot_as_file(module_name, '假名测试未通过')
            raise


'''

正式站答题不能确保完整解锁

    @pytest.mark.skipif(condition=skip_wsy_online, reason='跳过正式站测试开关打开，跳过正式站用例')
    @allure.story("假名复习测试用例")
    def test05_jiamingceshi_fuxi(self):
        self.jiaming.pagejiamingceshi_return_to_wsypage_keycode()
        self.jiaming.pagejiamingceshi_fuxi()

        while True:
            try:
                self.jiaming.pagejiamingceshi_click_fixed_answer()
            except:
                pass
            if self.jiaming.pagejiamingceshi_if_confirm_exist():
                self.jiaming.pagejiamingceshi_click_confirm()
                sleep(1)
                if self.jiaming.pagejiamingceshi_if_next_question_exist():
                    self.jiaming.pagejiamingceshi_click_next_question()
            if self.jiaming.pagejiamingceshi_if_question_exist():
                pass
            else:
                break

        try:
            log.info('正在进行断言')
            assert self.jiaming.base_find_element(page.study1_share_report)
            log.info('断言成功')
            self.jiaming.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.jiaming.base_get_screenshot_as_file(module_name, '假名测试未通过')
            raise
'''
